import { defineStore } from 'pinia';
import lodash , { isUndefined, isNull } from 'lodash';


import { getCurrentConfig } from '@/api/config'
import type { CityObj, ClueProductCluePlanObj, ChannelObj, ClueWorkObj  } from "@/api/config";
export interface DictionaryState {
   cityTree: CityObj[],
   cityList: CityObj[],
   clueStage: any,
   clueWork: ClueWorkObj
   clueCreditStatus: any,
   channel: ChannelObj,
   refreshStatus: boolean, //是否已经请求数据
   baitiao_quota_v2: any, // 白条
   huabei_quota_v2: any, //花呗
   productCluePlan: ClueProductCluePlanObj
}


const useDictStore = defineStore('dictionary', {
  state: (): DictionaryState => ({
    cityTree: undefined,
    cityList: undefined,
    refreshStatus: undefined,
    productCluePlan: undefined,
    channel: undefined,
    clueStage: undefined,
    clueWork: undefined,
    baitiao_quota_v2: undefined,
    clueCreditStatus: undefined,
    huabei_quota_v2: undefined
  }),
  getters: {
    getCityList(state: DictionaryState): CityObj[] {
      return state.cityTree;
    },
    getProductCluePlan(state: DictionaryState): ClueProductCluePlanObj {
      return { ...state.productCluePlan };
    },
    getClueWorkList(state: DictionaryState): ClueWorkObj {
      return { ...state.clueWork };
    },
    getRefreshStatus(state: DictionaryState): boolean{
       return state.refreshStatus;
    }
  },

  actions: {
    getCityObjByCode(code: string) {
      if(!isNull(this.cityList) && !isUndefined(this.cityList)){
        return lodash.find(this.cityList,(item)=> item.value === code);
      }
    },

    getCityNameByCode(code: string) {
      if(!isNull(this.cityList) && !isUndefined(this.cityList)){
        const findList = lodash.find(this.cityList,(item)=> item.value === code);
        return findList?.label || '';
      }
    },

        // 工作名称
    getClueWorkNameByCode(code: string){
      if(!isNull(this.clueWork) && !isUndefined(this.clueWork)){
        return this.clueWork[code] || '';
      }
    },

    // 信用状态
    getCreditStatusByCode(code: string){
      if(!isNull(this.clueCreditStatus) && !isUndefined(this.clueCreditStatus)){
        const itemObj = lodash.find(this.clueCreditStatus, (item)=> item.value === code);
        return itemObj?.label || '';
      }
    },

    // 白条状态
    getBaitiaoQuotaByCode(code: string){
      if(!isNull(this.baitiao_quota_v2) && !isUndefined(this.baitiao_quota_v2)){
        const itemObj = lodash.find(this.baitiao_quota_v2, (item)=> item.value === code);
        return itemObj.label || '';
      }
    },

    // 花呗状态
    getHuabeiQuotaByCode(code: string){
      if(!isNull(this.huabei_quota_v2) && !isUndefined(this.huabei_quota_v2)){
        const itemObj = lodash.find(this.huabei_quota_v2, (item)=> item.value === code);
        return itemObj.label || '';
      }
    },
    
 
    // 获取推送计划的字符串
    getPlanTextByKey(plan: string){
      return this.productCluePlan[plan];
    },

    // 设置字典配置
    setDictInfo(partial: Partial<DictionaryState>) {
      this.$patch(partial);
    },

    // 获取字典配置
    async getDictConfig() {
      
      try {

       const { data } = await getCurrentConfig();
       let cityArr: CityObj[] = [];
       const loopFun = (list: CityObj[])=>{
          let cityChildArr: CityObj[] = [];
          if(Array.isArray(list)){
            list.forEach((item)=>{
              cityChildArr.push({
                value: item?.value,
                label: item?.label
              });
              if(Array.isArray(item.children)){
                cityChildArr = cityChildArr.concat(loopFun(item.children))
              }
            })
          }
          return cityChildArr;
       }

       if(Array.isArray(data?.city)){
          data?.city.forEach((item)=>{
            cityArr.push({
              value: item?.value,
              label: item?.label
            });
            if(Array.isArray(item.children)){
              cityArr = cityArr.concat(loopFun(item.children))
            }
          })
       }

       this.setDictInfo({
         refreshStatus: true,
         cityTree: data?.city,
         cityList: cityArr || [],
         channel: data?.channel,
         clueStage: data?.clue?.stage,
         clueWork: data?.clue?.work,
         baitiao_quota_v2: data?.clue?.baitiao_quota_v2,
         huabei_quota_v2: data?.clue?.huabei_quota_v2,
         productCluePlan: data?.clue?.product_clue_plan,
         clueCreditStatus: data?.clue?.credit_status_v2,
       })
      } catch (err) {
        throw err;
      }
    },
  }

})


export default useDictStore;